<!-- 任务-SAP客户合并-->
<template>
  <div class="mdmDealerList" style="height: calc(100%-63px);">
    <CommonBtn label-name="SAP客户信息合并确认" :buttons="buttons" @onButtonClick="onButtonClick">
      <el-select slot="appendCondition" size="mini" v-model="conditions[1].value" @change="conditionChange">
        <el-option label="全部" value="0"></el-option>
        <el-option label="新客户" value="1"></el-option>
        <el-option label="待合并客户" value="2"></el-option>
      </el-select>
      <span slot="conditions" v-for="(item,index) of conditions" :key="index" v-if="item.checked && item.label && item.valueName != undefined">{{item.label+":"+item.valueName}}</span>
    </CommonBtn>
    <CommonList ref="list"
                v-if="!showEdit"
                :buttons="buttons"
                :columns="columns"
                :conditions="conditions"
                :listUrl="$api.API_SMMS_DEALER_LIST"
                @tableSelect="tableSelect"
                @dbClick="openItem"
                ></CommonList>
    <CustomerEdit :pageType="pageType"
                  ref="commonEdit"
                  :disabled="!enableEdit"
                  :label="editTitle"
                  v-if="showEdit"
                  :items="items"
                  :originalData="row"
                  @editBack="editBack"
                  @submit="submit"
                  @search="search"
                  @getSelectOption="getSelectOption"
                  />
    <CommonSearch :label="item.label"
                  :conditions="item.conditions"
                  v-for="(item, index) of searchs"
                  :key="index"
                  :listUrl="item.listUrl"
                  :columns="item.columns"
                  :items="item.items"
                  :page="item.page"
                  :originalData="item.searchData"
                  @search="search"
                  @submit="item.selectItem"
                  @searchBack="searchBack"/>
    <CommonExport :out-btn="button" :props="columns" :export_type="export_type"
                  @close="close" :export-url="$api.API_SMMSDEALER_EXPORT" :conditions="conditions"></CommonExport>
  </div>
</template>

<script>
    import http from '@/api/http'
    import {Message } from 'element-ui'
    import Tools from "@/util/Tools.js";
    import CommonSearch from '../../common/CommonSearch'
    import CommonList from "../../common/CommonList";
    import CommonBtn from "../../common/CommonBtn";
    import CustomerEdit from "./CustomerEdit";
    import CommonExport from "../../common/CommonExport";
    export default {
        name:'mdmDealerList',
        data(){
            return{
              searchData: {},
              searchItems: [],
              searchs:[],
                buttons:[
                    this.$config.BTNS.EXPORT,
                    this.$config.BTNS.SEARCH
                ],
                showEdit: false,
                editTitle: 'SAP客户信息确认',
                enableEdit: false,
                row: null,
                pageType: 8,
                button:null,
                conditions: [{
                  checked: true,
                  prop: 'pageType',
                  value: '8'
                },{
                  checked: true,
                  prop: 'mergeStatus',
                  value: '0'
                }],
                columns:[
                    {
                        prop:'smmsCode',
                        label:'统一编码'
                    },
                    {
                        prop:'smmsName',
                        label:'客户名称'
                    },
                    {
                        prop:'levelName',
                        label:'客户属性'
                    },
                    {
                        prop:'dataSource',
                        label:'数据来源'
                    },
                    {
                        prop:'smmsOwnerName',
                        label:'供货方名称'
                    },
                    {
                        prop:'count1',
                        label:'已合并客户数'
                    },
                    {
                        prop:'count2',
                        label:'待合并客户数'
                    },
                    {
                        prop:'createTime',
                        label:'创建时间'
                    },
                    {
                        prop:'modifyTime',
                        label:'最后修改时间'
                    }
                ],
                items:[{
                    name: '统一编码',
                    key: 'smmsCode',
                    type: 'text',
                    disable: true,
                    required: true,
                    placeholder: ''
                  },{
                    name: '客户属性',
                    key: 'levelName',
                    vkey: 'fkDealerLevel',
                    type: 'single-select',
                    required: true,
                    disable: true,
                    options: []
                  },{
                    name: '客户名称',
                    key: 'smmsName',
                    type: 'text',
                    placeholder: '',
                    required: true,
                    disable: true,
                  },
                  {
                    name: '客户负责人',
                    key: 'repName',
                    type: 'search',
                    required: true,
                    disable: false
                  }],
                export_type:[{
                    checked: true,
                    prop: 'pageType',
                    value: '8'
                }],
                button:null,
            }
        },
        methods:{
            onButtonClick(item) {
              let self = this
              if (item.event == 'search()') {
                this.searchs.push({
                  label: '查询客户信息',
                  page: 'customer',
                  searchData: this.searchData,
                  items: this.searchItems,
                  selectItem: function (formData, items) {
                    self.searchs.pop()
                    self.searchItems = items;
                    self.searchData = formData;
                    let data = {}
                    let conditions = self.conditions.filter(item=>{
                      return !item.label
                    })
                    // 回调
                    for (let idx in items) {
                      let item = items[idx]
                      if (item.checked == true) {
                        if (item.vkey) {
                          data[item.vkey] = formData[item.vkey]
                        } else {
                          data[item.key] = formData[item.key]
                        }
                        conditions.push({
                          checked: true,
                          label: item.name,
                          prop: item.vkey ? item.vkey : item.key,
                          valueName: formData[item.key],
                          value: item.vkey ? formData[item.vkey] : formData[item.key]
                        })
                      }
                    }
                    self.conditions = conditions
                  },
                })
              }
                this.button = item
            },
            conditionChange() {
                this.conditions = this.conditions.map(item=>{return item})
            },
            close(item) {
                this.button = item
            },
            tableSelect(row) {
              this.row = row
            },
            openItem(item) {
              // 打开详情
              this.showEdit = true
              this.enableEdit = true
            },
            editBack() {
              this.showEdit = false
            },
            close(item) {
                this.button = item
            },
            submit(formData) {
              http.post(this.$api.API_SMMS_DEALER_DEAL_COMBINE,formData)
                  .then(resp=>{
                    Message({type: 'success', message: 'SAP合并成功！'})
                    this.showEdit = false
                    this.row = null
                    this.conditionChange()
                  })
            },
            searchBack() {
              this.searchs.pop()
            },
            getSelectOption(item, formData) {
              console.log(item)
              if (item.key == "levelName") {
                Tools.getDealerLevels({pkid: formData ? formData.pkid:null, pageType: 0},data=>{
                  data.forEach(item => {
                    item.name = item.levelName
                    item.value = item.pkid
                  })
                  item.options = data
                })
              } else if (item.key == "businessStatus") {
                Tools.getBussinessStatus(data=>{
                  data.forEach(item => {
                    item.name = item.dvalue
                    item.value = item.dkey
                  })
                  item.options = data
                })
              } else if (item.key == "cooperationStatus") {
                Tools.getCooperationStatus(data=>{
                  data.forEach(item => {
                    item.name = item.dvalue
                    item.value = item.dkey
                  })
                  item.options = data
                })
              } else if (item.key == "tpFeeStatus") {
                Tools.getFeeStatus(data=>{
                  data.forEach(item => {
                    item.name = item.dvalue
                    item.value = item.dkey
                  })
                  item.options = data
                })
              } else if (item.key == "province") {
                // 地理城市
                Tools.getProvince(data => {
                  data.forEach(item => {
                    item.name = item.provinceName
                    item.value = item.provinceName
                  })
                  item.options = data
                })
              } else if (item.key == "physicalCity") {
                // 地理城市
                Tools.getCity({provinceName: formData.province},data => {
                  data.forEach(item => {
                    item.name = item.cityName
                    item.value = item.cityName
                  })
                  item.options = data
                })
              } else if (item.key == "adminCity") {
                // 地理城市
                Tools.getAdminCity({provinceName: formData.province, cityName: formData.physicalCity},data => {
                  console.log(data)
                  data.forEach(item => {
                    item.name = item.adminCity
                    item.value = item.adminCity
                    item.fkCityType = item.fkCityType
                  })
                  item.options = data
                })
              } else if (item.key == "destinationName") {
                Tools.getDestinations(data => {
                  data.forEach(item => {
                    item.name = item.destinationName
                    item.value = item.pkid
                  })
                  item.options = data
                })
              }
            },
            search(item, callback) {
              let self = this
              if (item.key == 'soldToCode') {
                // 客户信息
                this.searchs.push({
                  label: '客户信息',
                  conditions: [{checked: true, prop: 'pageType', value: '8'},{checked: true, prop: 'fkDealerLevel', value: this.$refs.commonEdit.formData.fkDealerLevel}],
                  listUrl: this.$api.API_SMMS_DEALER_LIST,
                  selectItem: function (item) {
                    self.searchs.pop()
                    console.log(item)
                    // 回调
                    if (typeof callback == 'function') {
                      callback(item)
                    }
                  },
                  items: [{
                    name: '客户名称',
                    key: 'smmsName',
                    type: 'text',
                  },{
                    name: 'SoldToCode',
                    key: 'soldToCode',
                    type: 'text',
                  },{
                    name: '统一编码',
                    key: 'smmsCode',
                    type: 'text',
                  },{
                    name: '客户属性',
                    key: 'levelName',
                    type: 'search',
                  },{
                    name: '客户集团',
                    key: 'customNameCn',
                    type: 'search',
                  },{
                    name: '客户负责人',
                    key: 'repName',
                    type: 'search',
                  }],
                  columns: [{
                    prop: 'smmsName',
                    label: '客户名称'
                  },{
                    prop: 'soldToCode',
                    label: 'SoldToCode'
                  },{
                    prop: 'smmsCode',
                    label: '统一编码'
                  },{
                    prop: 'levelName',
                    label: '客户属性'
                  },{
                    prop: 'customNameCn',
                    label: '客户集团'
                  },{
                    prop: 'repName',
                    label: '客户负责人'
                  }]
                })
              } else if (item.key == 'levelName') {
                // 客户属性
                this.searchs.push({
                  label: '客户属性管理',
                  conditions: [],
                  listUrl: this.$api.API_CUSTOMERTYPE,
                  selectItem: function (item) {
                    self.searchs.pop()
                    // 回调
                    if (typeof(callback) == 'function') {
                      callback({levelName: item.levelName, fkDealerLevel: item.pkid})
                    }
                  },
                  items: [{
                    name: '客户属性名称',
                    key: 'levelName',
                    type: 'text',
                  },{
                    name: '客户属性代码',
                    key: 'levelCode',
                    type: 'text',
                  },{
                    name: '是否有效',
                    key: 'isValid',
                    type: 'checkbox',
                  }],
                  columns: [{
                    prop: 'levelName',
                    label: '客户属性名称'
                  },{
                    prop: 'levelCode',
                    label: '客户属性代码'
                  },{
                    prop: 'valid',
                    label: '是否有效'
                  }]
                })
              } else if (item.key == 'repName') {
                // 客户属性
                this.searchs.push({
                  label: '人员信息',
                  conditions: [],
                  listUrl: this.$api.API_GETTPINFO_LIST,
                  selectItem: function (item) {
                    self.searchs.pop()
                    // 回调
                    if (typeof(callback) == 'function') {
                      callback({repName: item.repName, fkRep: item.pkid})
                    }
                  },
                  items: [{
                    name: '员工姓名',
                    key: 'repName',
                    type: 'text',
                  },{
                    name: '员工账号',
                    key: 'repCode',
                    type: 'text',
                  },{
                    name: '所属管理层级',
                    key: 'positionName',
                    type: 'search',
                  },{
                    name: 'WWID',
                    key: 'wWID',
                    type: 'text',
                  },{
                    name: 'SAP代码',
                    key: 'sapCode',
                    type: 'text',
                  },{
                    name: '总部/大区',
                    key: 'hQOrRegion',
                    type: 'text',
                  }],
                  columns: [{
                    prop: 'repName',
                    label: '员工姓名'
                  },{
                    prop: 'repCode',
                    label: '员工账号'
                  },{
                    prop: 'positionName',
                    label: '所属管理层级'
                  },{
                    prop: 'WWID',
                    label: 'wWID'
                  },{
                    prop: 'sapCode',
                    label: 'SAP代码'
                  },{
                    prop: 'hQOrRegion',
                    label: '总部/大区'
                  }]
                })
              } else if (item.key == 'customNameCn') {
                // 客户集团
                this.searchs.push({
                  label: '客户集团',
                  conditions: [],
                  listUrl: this.$api.API_SELECT_CUSTOMER_GROUP_INFO,
                  selectItem: function (item) {
                    self.searchs.pop()
                    // 回调
                    if (typeof(callback) == 'function') {
                      callback({customNameCn: item.customNameCN, fkCustom: item.pkid})
                    }
                  },
                  items: [{
                    name: '集团中文名称',
                    key: 'customNameCN',
                    type: 'text',
                  },{
                    name: 'Destination',
                    key: 'destinationName',
                    type: 'text',
                  },{
                    name: '集团英文名称',
                    key: 'customNameEN',
                    type: 'text',
                  },{
                    name: '预算Account',
                    key: 'accountCode',
                    type: 'text',
                  },{
                    name: '客户集团分类',
                    key: 'customTypeName',
                    type: 'search',
                  },{
                    name: '是否有效',
                    key: 'isValid',
                    type: 'checkbox',
                  }],
                  columns: [{
                    prop: 'customNameCN',
                    label: '集团中文名称'
                  },{
                    prop: 'destinationName',
                    label: 'Destination'
                  },{
                    prop: 'customNameEN',
                    label: '集团英文名称'
                  },{
                    prop: 'accountCode',
                    label: '预算Account'
                  },{
                    prop: 'customTypeName',
                    label: '客户集团分类'
                  },{
                    prop: 'valid',
                    label: '是否有效'
                  }]
                })
              } else if (item.key == 'positionName') {
                // 职位信息
                this.searchs.push({
                  label: '职位信息',
                  conditions: [],
                  listUrl: this.$api.API_MANAGEMENTHIERARCHY_LIST,
                  selectItem: function (item) {
                    self.searchs.pop()
                    // 回调
                    if (typeof(callback) == 'function') {
                      callback({positionName: item.positionName, fkPosition: item.pkid})
                    }
                  },
                  items: [{
                    name: '职位名称',
                    key: 'positionName',
                    type: 'text',
                  },{
                    name: '职位代码',
                    key: 'positionCode',
                    type: 'text',
                  }],
                  columns: [{
                    prop: 'positionName',
                    label: '职位名称'
                  },{
                    prop: 'createTime',
                    label: '创建时间'
                  },{
                    prop: 'modifyTime',
                    label: '最后修改时间'
                  },{
                    prop: 'isValid',
                    label: '是否有效'
                  },{
                    prop: 'positionCode',
                    label: '职位代码'
                  }]
                })
              } else if (item.key == 'customTypeName') {
                // 客户集团分类
                this.searchs.push({
                  label: '客户集团分类',
                  conditions: [],
                  listUrl: this.$api.API_CUSTOMERCLASSIFY_LIST,
                  selectItem: function (item) {
                    self.searchs.pop()
                    // 回调
                    if (typeof(callback) == 'function') {
                      callback({customTypeName: item.customTypeName, fkCustomType: item.pkid})
                    }
                  },
                  items: [{
                    name: '集团类型编码',
                    key: 'customTypeCode',
                    type: 'text',
                  },{
                    name: '集团类型名称',
                    key: 'customTypeName',
                    type: 'text',
                  },{
                    name: '是否有效',
                    key: 'isValid',
                    type: 'checkbox',
                  }],
                  columns: [{
                    prop: 'customTypeCode',
                    label: '集团类型编码'
                  },{
                    prop: 'customTypeName',
                    label: '集团类型名称'
                  },{
                    prop: 'valid',
                    label: '是否有效'
                  }]
                })
              }
            }
        },
        components:{CommonExport,CommonList,CommonBtn,CustomerEdit, CommonSearch}
    }
</script>

<style lang="less">

</style>
